﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Xml;


namespace ExcelToData
{
    public class Filer : BaseSinglton<Filer>
    {
        /// <summary>
        /// 检查创建文件夹
        /// </summary>
        /// <param name="filePath"></param>
        public void CheckAndCreateDirection(string filePath)
        {
            FileInfo info = new FileInfo(filePath);

            if (!Directory.Exists(info.Directory.FullName))
            {
                Directory.CreateDirectory(info.Directory.FullName);
            }
        }

        public bool IsExists(string path)
        {
            return File.Exists(path);
        }

        #region TEXT
        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public string ReadTXTFile(string FilePath)
        {
            if (File.Exists(FilePath))
            {
                StreamReader stream = File.OpenText(FilePath);
                string content = stream.ReadToEnd();
                stream.Close();
                return content;
            }
            else
            {
                Console.WriteLine("文件不存在:" + FilePath);
            }
            return "";
        }

        /// <summary>
        /// 写入文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="content"></param>
        public void WriteTXTFile(string path, string content)
        {
            CheckAndCreateDirection(path);

            if (File.Exists(path)) //删除以前的文件
                File.Delete(path);

            StreamWriter writer;
            writer = File.CreateText(path);
            writer.Write(content);
            writer.Close();
        }

        #endregion

        #region Xml

        /// <summary>
        /// 读取xml
        /// </summary>
        /// <param name="XmlFilePath"></param>
        public XmlDocument ReadXmlFile(string XmlFilePath)
        {
            XmlDocument xml = new XmlDocument();
            string content = ReadTXTFile(XmlFilePath);
            xml.LoadXml(content);
            return xml;
        }



        #endregion


        public void DeleteFile(string path)
        {
            File.Delete(path);
            Console.WriteLine("删除存档操作完成!");
        }



        public byte[] ReadBinFile(string FilePath)
        {
            if (File.Exists(FilePath))
            {
                FileStream stream = File.OpenRead(FilePath);
                byte[] buffer = new byte[stream.ReadByte()];
                while (true)
                {
                    if (stream.Read(buffer, 0, buffer.Length) == 0)
                    {
                        break;
                    }
                }
                stream.Close();
                return buffer;
            }
            else
            {
                Console.WriteLine("文件不存在:" + FilePath);
            }
            return null;
        }

        /// <summary>
        /// 写入文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="content"></param>
        public void WriteBinFile(string path, byte[] content)
        {
            Console.WriteLine("创建文件" + path);
            CheckAndCreateDirection(path);

            if (File.Exists(path)) //删除以前的文件
                File.Delete(path);

            using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
            {
                stream.Write(content, 0, content.Length);
                stream.Close();
                stream.Dispose();
            }
        }

        /// <summary>
        /// 复制文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="targetPath"></param>
        public void CopyFile(string path, string targetPath)
        {
            CheckAndCreateDirection(targetPath);

            FileInfo file = new FileInfo(path);
            if (file.Exists)
            {
                file.CopyTo(targetPath, true);
            }
        }


        public Assembly LoadAssemblyFromFile(string fileFullName)
        {
            byte[] b = File.ReadAllBytes(fileFullName);
            Assembly asm = Assembly.Load(b);
            return asm;
        }
    }
}

